<%@ page pageEncoding="UTF-8"%>

<%@ taglib uri="/tags/struts-tiles" prefix="tiles"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<%@ taglib uri="/tags/struts-nested" prefix="nested"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<link rel='stylesheet' href='resources/css/jquerylib/jquery-ui-1.7.custom.css'/>
<link rel="stylesheet" href="resources/css/jquerylib/jquery.autocomplete.css" />
<script type='text/javascript' src='resources/js/jquerylib/jquery.autocomplete.js'></script>
<script type="text/javascript" src="resources/js/jquerylib/jquery.ui.core.js"></script>
<script type="text/javascript" src="resources/js/jquerylib/jquery.ui.datepicker.js"></script>
<script type="text/javascript" src="resources/js/jquerylib/jquery.ui.datepicker-zh-CN.js"></script>
<script type="text/javascript" src="dwr/interface/ServiceStudent.js"></script> 

<script>
	
	//---Initialization
	var current_page = 1;
	var page_size = 15;
	var total_page = "<logic:present name='student.page'><bean:write name='student.page' property='totalPage' /></logic:present><logic:notPresent name='student.page'>0</logic:notPresent>";
	
	// query with the search fields
	function query() {
		current_page = 1;
		getStudentPagination(current_page);
	}	
	
	// obtains the student with pagination
	function getStudentPagination(page_idx) {
		
		var student_no = $("#student_no").val();
		var student_cname = $("#student_cname").val();
		var student_status = $("#student_status option").filter(":selected").attr("value");
		student_status = student_status || null;
		
		var entity = {studentNo: student_no, studentCName: student_cname, studentStatus: student_status};
		
		$("#loading").css("display", "inline");
		
		ServiceStudent.getStudentPagination(entity, page_size, get_page_offset(page_idx), null, null, {
			callback: function(obj) {
				
				// remove all rows
				DWRUtil.removeAllRows("datable");
				
				// add rows against the callback data object
				DWRUtil.addRows("datable", obj.items, [
					function(data) {return data.studentNo;},
					function(data) {return data.studentCName ? data.studentCName : "/";},
					function(data) {return data.studentEName ? data.studentEName : "/";},
					function(data) {return data.studentSex == 1 ? '男' : '女';},
					function(data) {return data.studentStatusText;},
					function(data) {return data.studentAge;},
					function(data) {return data.studentSeniorSchool ? data.studentSeniorSchool : (data.studentMiddleSchool ? data.studentMiddleSchool : (data.studentPrimarySchool ? data.studentPrimarySchool : '/'));},
					function(data) {
						var wrapper = document.createElement("span");
						wrapper.className = "detail";
						
						// creates the a tag
						var alink = document.createElement("a");
						alink.href = "admin/maintenance/student_edit/" + data.id;
						alink.innerHTML = "编辑";
						
						wrapper.innerHTML = "[" + alink.outerHTML + "]";
						return wrapper;
					},
					function(data) {
						var wrapper = document.createElement("span");
						wrapper.className = "detail";
						
						// creates the a tag
						var alink = document.createElement("a");
						alink.href = "javascript:void(0);";
						alink.innerHTML = "删除";
						alink.id = "del_" + data.id;
						
						wrapper.innerHTML = "[" + alink.outerHTML + "]";
						return wrapper;
					}
				], {
					// row creator, aims to set the row id
					rowCreator:function(data) {
						var _row = document.createElement("tr");
						_row.id = "datarow" + data.rowData.id;
						if (data.rowIndex % 2 == 1) {
							_row.bgColor = "#fafafa";
						}
						return _row;
					}
				});
				
				// polish the alternative hover background color on rows
				polish_table_hover("datarow","active");
				
				// refresh the page index input text
				DWRUtil.setValue("rdpage", page_idx);
				
				// refresh the total page number
				DWRUtil.setValue("totalPage", obj.totalPage);
				
				// refresh the total page number
				DWRUtil.setValue("totalCount", obj.totalCount);
				
				// binds the delete method
				event_bind_del();
				
				// hidden loading
				$("#loading").css("display", "none");
			},
			timeout: 5000,
			errorHandler: function(message) {
				alert ("Error: " + message);
				
				$("#loading").css("display", "none");
			}
		});
	}
	
	// obtains the page offset with the specified page index
	function get_page_offset(page){
		page = page <= 0 ? 0 : page - 1;
		return page * page_size;
	}
	
	// move to the first page
	function move2first() {
		move2page(1);
	}
	
	// move to the previous page
	function move2previous() {
		move2page(current_page - 1);
	}
	
	// move to the next page
	function move2next() {
		move2page(current_page + 1);
	}
	
	// move to the tail page
	function move2tail() {
		move2page(total_page);
	}
	
	// move to the specified page with the given page index
	function move2page(idx) {
		
		var page_idx = idx || $("#rdpage").val();
		if(isNaN(page_idx) || page_idx == ""){
			$("#rdpage").focus();
			return;
		}
		
		// obtains the page index to be moved
		page_idx = parseInt(page_idx) > parseInt($("#totalPage").html()) ? $("#totalPage").html() : page_idx;
		
		// refresh the current page index
		current_page = page_idx;
		
		if (page_idx > 0) {
			getStudentPagination(page_idx);
		} else {
			alert("没有任何数据记录...");
		}
	}
	
	// remove student with the specified id
	function remove_student(id){
		if (confirm("您确定要删除该学生信息么?")) {
			ServiceStudent.removeStudentById(id, {
				callback: function(str) {
					// remove the entity row
					$("#datarow"+id).fadeOut();
				},
				timeout: 5000,
				errorHandler: function(message) {
					alert ("Error: " + message);
				}
			});
		}
	}
	
	// event binder on entity deletion
	function event_bind_del() {
		$("a[id^=del_]").unbind("click");
		$("a[id^=del_]").click(function(){
			var id = $(this).attr("id").substr("del_".length);
			remove_student(id);
		});
	}
	
	function frm_enter(event){ 
		e = event ? event :(window.event ? window.event : null); 
		if(e.keyCode==13){  
			$("#search").trigger("click");
		}
	}
	
	$(function(){
		// polish the alternative table row hover color
		polish_table_hover("datarow","active");
	});
	
</script>

<div id="nav">
	<p><b>位置：</b>首页 < 学生管理 < 学生档案</p>
</div>

<div id="content">
	
	<div class="box" id="searchbox">
		<fieldset style="border:1px solid #CDE0F5;">
			<legend class="bnr searchbnr">查询条件</legend>
			<div>
				<table cellpadding="10">
					<col width="180"></col>
					<col width="180"></col>
					<col width="180"></col>
					<tr>
						<td><label>学号：</label><input type="text" class="text" size="13" id="student_no" value="${studentForm != null && studentForm.studentNo != null ? studentForm.studentNo : ''}"><img id="_loading_student_no" src="resources/images/layout/loading.gif" style='display:none' align='absmiddle' /></td>
						<td><label>姓名：</label><input type="text" class="text" size="13" id="student_cname" value="${studentForm != null && studentForm.studentCName != null ? studentForm.studentCName : ''}"><img id="_loading_student_cname" src="resources/images/layout/loading.gif" style='display:none' align='absmiddle' /></td>
						<td><label>学生状态：</label><select id="student_status"><option value="">全部</option><option value="0">流失</option><option value="1">已报名</option><option value="2">在读</option><option value="3">休学</option></select></td>
						<td>
							<a href="javascript:void(0);" id="search" title="开始搜索" onclick="javascript:query();"><img src='resources/images/layout/searchan.gif' style='margin-left: 9px;' /></a>
						</td>
					</tr>
				</table>
			</div>
		</fieldset>
	</div>

	<div class="box">
		<p class="bnr classbnr">
			查询结果
			<img src="resources/images/layout/loading.gif" align="absmiddle" id="loading" style="display:none;float: right;margin-right: 2px;" />
		</p>
		<div id="datagrid">
			<%@ include file="datagrid/datagridStudent.jsp"%>
		</div>
	</div>

</div>